# -*- coding: utf-8 -*-
# @Author: Wolf
# @Date:   2025-03-20 11:11:11
# @Last Modified by:   Wolf
# @Last Modified time: 2025-03-20 18:18:12
from django.shortcuts import render, HttpResponse,redirect
from app01.models import Department,UserInfo

# Create your views here.
def index(request):
    return HttpResponse("欢迎使用")

def user_list(request):
    return render(request, "user_list.html")

def user_add(request):
    return render(request, "user_add.html")

def tpl(request):
    name= "wolf"
    roles = ["管理员", "CEO", "保安"]
    user_info = {"name": "郭智", "salary": 100000, 'role': "CTO"}
    return render(
        request,
        "tpl.html",
        {
            "n1": name,
            "n2": roles,
            "n3": user_info,
            }
        )

def login(request):
    if request.method == "GET":
        return render(request, "login.html")
    else:
        print(request.POST)
        username = request.POST.get("user")
        password = request.POST.get("pwd")
        if username == 'root' and password == "123":
            #return HttpResponse("登录成功")
            return redirect("https://www.baidu.com/")
        else:
            return render(request, "login.html",{"error_msg": "用户名密码错误"})
        

def orm(request):
    # Department.objects.create(title="技术部")
    # Department.objects.create(title="销售部")
    # Department.objects.create(title="财务部")

    return HttpResponse("成功")


def info_list(request):
    data_list = UserInfo.objects.all()
    print(data_list)

    return render(request, "info_list.html", {"data_list": data_list})


def info_add(request):
    if request.method == "GET":
        return render(request, 'info_add.html')
    else:
        name = request.POST.get("name")
        password = request.POST.get("password")
        age = request.POST.get("age")

        UserInfo.objects.create(name=name, password=password, age=age)

        #return HttpResponse("成功添加")
        return redirect("/info/list/")
    



